* This code computes foreclosure rates in the ASMB
* It also examines negative equity among "mildly" and "severely" delinquent 
* foreclosures, as those terms are used in Section B.3.

clear all

********************************
** get total number of mortgages active in 2017 from NMDB


use "$NMDB/nmdb_20210113_v130_month1.dta", clear

drop if close_dt<=20161231
drop if open_dt>=20180101

count

* this leaves 2,810,234 mortgages.  Since NMDB is a 1-in-20 sample, that implies
* there were 56,204,680 first-lien closed-end mortgages active at some point in 2017


********************************
** get numbers from ASMB



use "$DATA_OUT/NMDB_ASMB_respondents", clear
merge 1:1 asmbid using "$DATA_OUT/NMDB_ASMB_monthly", assert(match) keepusing(perfm*) nogen // bring in monthly loan performance information
svyset [pweight = analysis_weight]

* Get foreclosure rate in ASMB
svy: tab foreclosure

* Count foreclosures in the delinquent sample that are dropped because they haven't been current recently enough
count if foreclosure==1 & heavywgt==0 & in_default_sample==0

* Count "current" foreclosures
count if foreclosure==1 & heavywgt==1


*** Keep only foreclosures in the delinquent subsample, to align with the main body of the paper
keep if heavywgt==0
keep if foreclosure==1

*** Compare negative equity proxies among those not in default sample because they haven't been current recently enough
svy: tab EUW_or_disaster if in_default_sample==0 //for reference, report how many foreclosures not in the default sample were EUW
svy: tab EUW_or_disaster if in_default_sample==1 //for reference, report how many foreclosures in the default sample were EUW



** Identify borrowers only slightly delinquent at the time the survey sample was chosen
gen stat_at_start = statq68 if survey_year==2016
replace stat_at_start = statq72 if survey_year==2017
replace stat_at_start = statq76 if survey_year==2018

gen perfm_at_start = perfm205 if survey_year==2016
replace perfm_at_start = perfm217 if survey_year==2017
replace perfm_at_start = perfm229 if survey_year==2018


********************************
*** Now label "mildly" delinquent foreclosures in the ASMB
keep if in_default_sample


* Create a string that tracks performance right after the survey sample was chosen
gen perfm_string = ""
forvalues mm=1(1)6 {
	foreach sy in 2016 2017 2018 {
		
		local year_adj = (`sy' - 2016 ) * 12
		
		*local month_num = 205 + `mm' + `year_adj'
		local month_num = 198 + `mm' + `year_adj'

		
		replace perfm_string = perfm_string + perfm`month_num' if survey_year==`sy'
	}
}



svy: tab perfm_at_start


* Now identify those foreclosures that were current within six months of the survey stratification
gen mild_delinq_at_start = regexm(perfm_string,"C")
svy: tab mild_delinq_at_start



gen EUW_MTM = LTV_FHFA_tract>=90 & !mi(LTV_FHFA_tract)


svy: tab EUW_or_disaster if mild==0
svy: tab EUW_or_disaster if mild==1

